//
//  RootViewController.m
//  UITest
//
//  Created by wp w on 12-6-6.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "RootViewController.h"
#import "DLog.h"
#import "SimpleViewController.h"
#import "AnimationSimpleViewController.h"
#import "AnimationRoundedCornerViewController.h"
#import "Animation2RoundedCornerViewController.h"
#import "ImgSimpleViewController.h"
#import "ImgAnimationViewController.h"
#import "ScroolSimpleViewController.h"
#import "ScrollScaleViewController.h"
#import "UIPageControlViewController.h"
#import "UILabelViewController.h"
#import "ProgressViewController.h"
#import "ActivityViewController.h"
#import "AlertViewAutohideViewController.h"
#import "AlertViewNoBtnViewController.h"
#import "UIActionSheetViewController.h"
#import "UIToolbarViewController.h"
#import "UITextFieldViewController.h"
#import "UITextViewSimpleViewController.h"
#import "UITextViewScrollViewController.h"
#import "UIButtonViewController.h"
#import "UISwitchViewController.h"
#import "UISliderViewController.h"
#import "UISegmentedControlViewController.h"
#import "UIWebViewController.h"
#import "UIWebViewLocalViewController.h"
#import "ViewRotateViewController.h"
#import "ViewRotate2ViewController.h"
#import "UIButton2ViewController.h"
#import "TapViewController.h"
#import "SwipeViewController.h"
#import "JSViewController.h"

@implementation RootViewController
@synthesize dict;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"UITest";

    self.dict = [[[NSDictionary alloc] initWithContentsOfFile:[self getPlistPath:@"my"]] autorelease];
    Log(@"%@",dict);
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.dict = nil;
}

- (void)dealloc{
    [dict release];
    [super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

//有多少个section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.dict allKeys]count];
}

//每个scetion有多少row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [[self.dict allKeys] objectAtIndex:section];
    return [[self.dict objectForKey:key]count];
}

//画每个cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    NSInteger section = [indexPath section];
    NSInteger row = [indexPath row];
    NSString *key = [[self.dict allKeys]objectAtIndex:section];
    NSArray *objs = [self.dict objectForKey:key];
    NSString *obj = [objs objectAtIndex:row];
    cell.textLabel.text = obj;
//    cell.textLabel.font = [UIFont systemFontOfSize:12];
    
    return cell;
}

//画每个section的header
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [[self.dict allKeys]objectAtIndex:section];
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = [indexPath section];
    NSInteger row = [indexPath row];
    NSString *key = [[self.dict allKeys]objectAtIndex:section];
    NSArray *objs = [self.dict objectForKey:key];
    NSString *obj = [objs objectAtIndex:row];
    LogStr(key);
    LogStr(obj);
    
    UIViewController* controller = nil;
    if ([key isEqualToString:@"subview"]) {
        if ( [obj isEqualToString:@"subview-simple"] ) {
            controller = [[[SimpleViewController alloc] init] autorelease];
        }
        else if ( [obj isEqualToString:@"subview-Animation-simple"] ) {
            controller = [[[AnimationSimpleViewController alloc] init] autorelease];
        }
        else if ( [obj isEqualToString:@"subview-Animation-roundedCorner"] ) {
            controller = [[[AnimationRoundedCornerViewController alloc] init] autorelease];
        }
        else if ( [obj isEqualToString:@"subview-Animation2-roundedCorner"] ) {
            controller = [[[Animation2RoundedCornerViewController alloc] init] autorelease];
        }
    }
    else if ([key isEqualToString:@"views"]){
        if ( [obj isEqualToString:@"UIImageView-simple"] ) {
            controller = [[[ImgSimpleViewController alloc] init] autorelease];
        }
        else if ( [obj isEqualToString:@"UIImageView-animation"] ) {
            controller = [[[ImgAnimationViewController alloc] init] autorelease];
        }
        else if ( [obj isEqualToString:@"UIScrollView-simple"] ) {
            controller = [[[ScroolSimpleViewController alloc] init] autorelease];
        }
        else if ( [obj isEqualToString:@"UIScrollView-scale"] ) {
            controller = [[[ScrollScaleViewController alloc] init] autorelease];
        }
        else if ( [obj isEqualToString:@"UIProgressView-simple"] ) {
            controller = [[[ProgressViewController alloc] init] autorelease];
        }
        else if ( [obj isEqualToString:@"UIActivityIndicatorView"] ) {
            controller = [[[ActivityViewController alloc] init] autorelease];
        }
        else if ( [obj isEqualToString:@"UIAlertView-autohide"] ) {
            controller = [[[AlertViewAutohideViewController alloc] init] autorelease];
        } 
        else if ( [obj isEqualToString:@"UIAlertView-nobutton"] ) {
            controller = [[[AlertViewNoBtnViewController alloc] initWithNibName:@"AlertViewNoBtnViewController" bundle:nil] autorelease];
        }
        else if ( [obj isEqualToString:@"UITextView-simple"] ) {
            controller = [[[UITextViewSimpleViewController alloc] init] autorelease];
        }
        else if ( [obj isEqualToString:@"UITextView-scroll"] ) {
            controller = [[[UITextViewScrollViewController alloc] init] autorelease];
        }
        else if ( [obj isEqualToString:@"UIWebView-simple"] ) {
            controller = [[[UIWebViewController alloc] init] autorelease];
        }
        else if ( [obj isEqualToString:@"UIWebView-local"] ) {
            controller = [[[UIWebViewLocalViewController alloc] init] autorelease];
        }
        else if ( [obj isEqualToString:@"UIWebView-JS"] ) {
            controller = [[[JSViewController alloc] init] autorelease];
        }
        
    }
    else if ([key isEqualToString:@"control"]){
        if ( [obj isEqualToString:@"UIPageControl"] ) {
            controller = [[[UIPageControlViewController alloc] init] autorelease];
        }
        else if ( [obj isEqualToString:@"UILabel"] ) {
            controller = [[[UILabelViewController alloc] initWithNibName:@"UILabelViewController" bundle:nil] autorelease];
        }  
        else if ( [obj isEqualToString:@"UIButton"] ) {
            controller = [[[UIButtonViewController alloc] init] autorelease];
        } 
        else if ( [obj isEqualToString:@"UIButton-custon-animation"] ) {
            controller = [[[UIButton2ViewController alloc] init] autorelease];
        } 
        else if ( [obj isEqualToString:@"UITextField"] ) {
            controller = [[[UITextFieldViewController alloc] init] autorelease];
        } 
        else if ( [obj isEqualToString:@"UISwitch"] ) {
            controller = [[[UISwitchViewController alloc] init] autorelease];
        } 
        else if ( [obj isEqualToString:@"UISlider"] ) {
            controller = [[[UISliderViewController alloc] init] autorelease];
        } 
        else if ( [obj isEqualToString:@"UISegmentedControl"] ) {
            controller = [[[UISegmentedControlViewController alloc] init] autorelease];
        }
        else if ( [obj isEqualToString:@"UIActionSheet"] ) {
            controller = [[[UIActionSheetViewController alloc] init] autorelease];
        } 
        else if ( [obj isEqualToString:@"UIToolbar"] ) {
            controller = [[[UIWebViewLocalViewController alloc] init] autorelease];
        }
    }
    else if ([key isEqualToString:@"view action"]){
        if ( [obj isEqualToString:@"Rotate"] ) {
            controller = [[[ViewRotateViewController alloc] init] autorelease];
        }
        else if ( [obj isEqualToString:@"Rotate2"] ) {
            controller = [[[ViewRotate2ViewController alloc] init] autorelease];
        } 
    }
    else if ([key isEqualToString:@"UI*GestureRecognizer"]){
        if ( [obj isEqualToString:@"tap"] ) {
            controller = [[[TapViewController alloc] init] autorelease];
        }
        else if ( [obj isEqualToString:@"swipe"] ) {
            controller = [[[SwipeViewController alloc] init] autorelease];
        }
        else if ( [obj isEqualToString:@"tap"] ) {
            controller = [[[SwipeViewController alloc] init] autorelease];
        }
    }
    
    [self.navigationController pushViewController:controller animated:YES];
}

#pragma mark - wwp
- (NSString *)getPlistPath:(NSString *)str{
    //获取plist路径
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *path = [bundle pathForResource:str ofType:@"plist"];
    LogStr(path);
    return path;
}


@end
